<?php

/**
 * 节点对象
 */
class Node
{
    public $data;//节点数据
    public $next;//后继节点
    
    public function __construct($data)
    {
        $this->data = $data;
    }
}

/**
 * 判断链表是否有环
 */
function detectCycle($node)
{
    $node1 = $node->next->next;
    $node2 = $node->next;
    while($node1 && $node2)
    {
        
        if ($node1 == $node2)
        {
            return true;
        }
        
        $node1 = $node1->next->next;
        $node2 = $node2->next;
    }
    
    return false;
}


$node1 = new Node(1);
$node2 = new Node(2);
$node3 = new Node(3);
$node4 = new Node(4);
$node5 = new Node(5);
$node6 = new Node(6);
$node1->next = $node2;
$node2->next = $node3;
$node3->next = $node4;
$node4->next = $node5;
$node5->next = $node3;
var_dump(detectCycle($node1));



